/* 新增顾客财产 */
<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    :bodyStyle="bodyStyle"
    style="top:15%"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品序号">
            <a-input placeholder="请输入唯一序号" v-decorator.trim="['serialNum']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
            <a-input placeholder="请输入客户名称" v-decorator.trim="['customer', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2" v-if="recordType == 1">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品名称">
            <a-select
              v-decorator.trim="['itemName', validatorRules.name]"
              allowClear
              placeholder="请选择"
              showSearch
              optionFilterProp="children"
              @select="onSelect"
            >
              <a-select-option v-for="(item, index) in getStockData" :key="index" :value="item.itemName">
                {{ item.itemName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2" v-else>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品名称">
            <a-input placeholder="请输入产品名称" v-decorator.trim="['itemName', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="规格">
            <a-input placeholder="请输入规格" v-decorator.trim="['specifications']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单位">
            <a-input placeholder="请输入单位" v-decorator.trim="['unit']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数量">
            <a-input-number
              placeholder="请输入数量"
              style="width:100%"
              v-decorator.trim="['quantity', validatorRules.name]"
              :min="0"
              :max="onlyQuantity"
              @change="numChange"
            />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单价">
            <a-input placeholder="请输入单价" v-decorator.trim="['price']" />
          </a-form-item>
        </a-col> -->
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总金额">
            <a-input placeholder="请输入数量" v-decorator.trim="['totalAmount']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2">
          <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['recordDate', validatorRules.name]"
              format="YYYY-MM-DD"
              placeholder="请选择日期"
            />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item label="仓库管理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入仓库管理人" v-decorator.trim="['depotManager']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item
            :label="this.$router.currentRoute.name == '顾客财产出库' ? '出库负责人' : '入库负责人'"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-input placeholder="请输入入库负责人" v-decorator.trim="['principal']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea placeholder="请输入备注" v-decorator.trim="['remark']" auto-size />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { addgkcc, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { getAction } from '../../../api/manage'
import { message } from 'ant-design-vue'
message.config({
  duration: 2, // 持续时间
  top: `100px`, // 到页面顶部距离
  maxCount: 3 // 最大显示数, 超过限制时，最早的消息会被自动关闭
})
export default {
  name: 'addCustomerProperty',
  mixins: [JeecgListMixin],
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      defaultFileList: null,
      isReadOnly: false,
      recordType: '',
      onlyQuantity: 9999999999,
      getStockData: [],
      isOk: [
        { id: 0, name: '合格' },
        { id: 1, name: '不合格' }
      ],
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.5 + 'px',
        'overflow-y': 'auto'
      },
      labelCol: {
        xs: { span: 24 / 2 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 / 2 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [{ required: true, message: '请输入!' }]
        }
      }
    }
  },
  created() {},
  methods: {
    numChange(value) {
      console.log('changed', value)
      if (value > this.onlyQuantity && this.recordType == 1) {
        this.$message.warning('该产品最大库存为' + this.onlyQuantity)
      }
    },
    onSelect(e, val) {
      console.log(e, 'eeeee')
      console.log(val.data.key, 'val')
      console.log(this.getStockData[val.data.key].quantity)
      if (this.getStockData[val.data.key].quantity == 0) {
        this.$message.warning('库存为0,暂不能出库')
      }
      this.onlyQuantity = this.getStockData[val.data.key].quantity
    },
    add(e) {
      this.edit()
      if (e == 1) {
        this.getStock()
        this.recordType = 1
      }
      console.log(e, 'eee')
    },
    getStock() {
      getAction('	/depotProduct/getPage?itemType=' + 4).then(res => {
        console.log(res, 'getStock')
        this.getStockData = res.data.records
      })
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      console.log(this.model)
      this.$nextTick(() => {
        if (record) {
          var recordDate = this.$moment(this.model.recordDate)
          this.form.setFieldsValue({ recordDate: recordDate })
        }
        this.form.setFieldsValue(
          pick(
            this.model,
            'serialNum',
            'status',
            'recordType',
            'itemName',
            'itemNum',
            'specifications',
            'unit',
            'quantity',
            'price',
            'batchNumber',
            // 'purpose',
            'supplier',
            // 'totalAmount',
            'depotManager',
            'status',
            'remark',
            'principal',
            'unqualifiedStock'
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          console.log(this.model)
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          formData.id = this.model.id
          //3为 贵金属/化工
          formData.itemType = 4
          if (this.$router.currentRoute.name == '顾客财产出库') {
            formData.recordType = 1
          } else {
            formData.recordType = 2
          }
          formData.recordDate = this.$moment(formData.recordDate).format('YYYY-MM-DD')
          let obj
          if (!this.model.id) {
            obj = addgkcc(formData)
          } else {
            obj = addgkcc(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                this.$message.success('保存成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.data || '操作失败')
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
